<template>
    <list-layout>
        <div slot="search">
            <el-form :model="queryParams" :inline="true" ref="queryForm" v-show="showSearch" label-width="88px">
                <el-row>
                    <el-col :span="6">

                        <el-form-item label="组织编码" prop="assetsCode">
                            <el-input v-model="queryParams.assetsCode" :placeholder="$t('public.placeholder',{placeholder: '组织编码'})" clearable size="small" @keyup.enter.native="handleQuery" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="客户" prop="assetsCode">
                            <el-input v-model="queryParams.assetsCode" :placeholder="$t('public.placeholder',{placeholder: '客户'})" clearable size="small" @keyup.enter.native="handleQuery" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="产品编码" prop="assetsCode">
                            <el-input v-model="queryParams.assetsCode" :placeholder="$t('public.placeholder',{placeholder: '产品编码'})" clearable size="small" @keyup.enter.native="handleQuery" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6" align="left">
                        <el-form-item class="right-btns">
                            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{$t('public.query')}}</el-button>
                            <el-button icon="el-icon-refresh" class="resectBtn" size="mini" @click="resetQuery">{{$t('public.reset')}}</el-button>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>

        </div>
        <div class="app-container">
            <el-row :gutter="10" class="action-btns mb8 btnGroup">
                <el-col :span="1.5">
                    <el-button v-hasPermi="['masterdata:process:add']" type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">{{ $t('btn.add') }}
                    </el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button v-hasPermi="['masterdata:process:edit']" type="primary" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate">{{ $t('btn.edit') }}
                    </el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button v-hasPermi="['masterdata:process:remove']" type="primary" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete">{{ $t('btn.delete') }}
                    </el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button class="importitemBtn" size="mini" @click="handleImport" v-hasPermi="['masterdata:checkoutUnit:import']"><img src="@/assets/images/icons/daoru.png" alt=""> {{$t('public.import')}}</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button v-hasPermi="['masterdata:process:export']" type="primary" icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExport">{{ $t('btn.export') }}
                    </el-button>
                </el-col>
                <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
            </el-row>

            <el-table border stripe v-loading="loading" :data="processList" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" align="center" />
                <el-table-column :label="$t('public.serialNum')" type="index" align="center">
                    <template slot-scope="scope">
                        <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
                    </template>
                </el-table-column>
                <el-table-column :label="$t('public.opera')" align="center" class-name="small-padding fixed-width">
                    <template slot-scope="scope">
                        <div class="action-wrap">
                            <table-body-btn type="edit" @click.native="handleUpdate(scope.row)" v-hasPermi="['masterdata:process:edit']" />
                            <table-body-btn type="delete" @click.native="handleDelete(scope.row)" v-hasPermi="['masterdata:process:remove']" />
                        </div>
                    </template>
                </el-table-column>
                <!--      <el-table-column label="ID" align="center" prop="id" />-->
                <!-- <el-table-column :label="$t('process.organizeCode')" align="center" prop="organizeCode" show-overflow-tooltip />
                <el-table-column :label="$t('process.organizeName')" align="center" prop="organizeName" show-overflow-tooltip /> -->
                <el-table-column label="组织编码" align="center" prop="processName" show-overflow-tooltip />
                <el-table-column label="客户" align="center" prop="processCode" show-overflow-tooltip />
                <el-table-column label="产品编码" align="center" prop="remark" show-overflow-tooltip />
                <el-table-column label="说明" align="center" prop="processStatus" show-overflow-tooltip />
                <el-table-column label="最后更新人" align="center" prop="createBy" show-overflow-tooltip />
                <el-table-column label="最后更新时间" align="center" prop="createTime" show-overflow-tooltip />

            </el-table>

            <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
            <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body :fullscreen="fullscreen" :close-on-click-modal="false">
                <DialogTit slot="title" :title="title" :fullscreen.sync="fullscreen"></DialogTit>
                <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                    <!-- <el-form-item size="small" :label="$t('process.organizeId')" prop="organizeId">
                        <el-select v-model="form.organizeId" :placeholder="$t('tips.pleaseSelectInput') + $t('process.organizeId')" style="width:100%">
                            <el-option v-for="organizes in organizesList" :key="organizes.id" :label="organizes.organizeName" :value="organizes.id">{{ organizes.organizeName }} - {{ organizes.organizeCode }}
                            </el-option>
                        </el-select>
                    </el-form-item> -->
                    <el-form-item size="small" label="客户" prop="processName">
                        <el-input v-model="form.processName" :placeholder="$t('tips.pleaseInput') + '客户'" />
                    </el-form-item>
                    <el-form-item size="small" label="产品编码" prop="processCode">
                        <el-input v-model="form.processCode" :placeholder="$t('tips.pleaseInput') + '产品编码'" />
                    </el-form-item>
                    <el-form-item size="small" label="产品名称" prop="remark">
                        <el-input v-model="form.remark" :placeholder="$t('tips.pleaseInput') + '产品名称'" />
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="cancel" size="small" icon="el-icon-close">{{$t('btn.cancel')}}</el-button>
                    <el-button type="primary" @click="submitForm" size="small" icon="el-icon-check">{{ $t('btn.confirm') }}</el-button>
                </div>
            </el-dialog>
            <import-dialog :visible.sync="unitVisible" @callback="getList" :action="'/qcmasterdata/process/import'" :templateUrl="'/static/unit.xlsx'" />
        </div>
    </list-layout>
</template>

<script>
// import {
//     listProcess,
//     getProcess,
//     delProcess,
//     addProcess,
//     updateProcess,
//     exportProcess,
// } from '@/api/basic/process'
import { listOrganizeData } from '@/api/basic/organize'
import importDialog from '../../components/importDialog'
export default {
    name: 'Process',
    components: { importDialog },
    data() {
        return {
            fullscreen: false,
            // 组织
            organizesList: [],
            // 遮罩层
            loading: false,
            // 导出遮罩层
            exportLoading: false,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 产品线表格数据
            processList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                organizeId: null,
                processName: null,
            },
            unitVisible: false,
            // 表单参数
            form: {},
            // 表单校验
            // rules: {
            //     organizeId: [
            //         {
            //             required: true,
            //             message:
            //                 this.$t('process.organizeId') +
            //                 this.$t('process.notNull'),
            //             trigger: 'blur',
            //         },
            //     ],
            //     processName: [
            //         {
            //             required: true,
            //             message:
            //                 this.$t('process.processName') +
            //                 this.$t('process.notNull'),
            //             trigger: 'blur',
            //         },
            //         {
            //             min: 0,
            //             max: 100,
            //             message:
            //                 this.$t('process.processName') +
            //                 this.$t('process.superLong'),
            //             trigger: 'blur',
            //         },
            //     ],
            //     remark: [
            //         {
            //             min: 0,
            //             max: 255,
            //             message:
            //                 this.$t('process.remark') +
            //                 this.$t('process.superLong'),
            //             trigger: 'blur',
            //         },
            //     ],
            // },
        }
    },
    created() {
        this.getList()
        this.getListOrganize()
    },
    methods: {
        handleImport() {
            this.unitVisible = true
            // this.$refs['excel-upload-input'].click()
        },
        /** 查询产品线列表 */
        getList() {
            // this.loading = true
            // listProcess(this.queryParams).then((response) => {
            //     this.processList = response.rows
            //     this.total = response.total
            //     this.loading = false
            // })
        },
        // 取消按钮
        cancel() {
            this.open = false
            this.reset()
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                organizeId: null,
                processName: null,
                remark: null,
                delFlag: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null,
            }
            this.resetForm('form')
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm('queryForm')
            this.handleQuery()
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.processId)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset()
            this.open = true
            this.title = '新增客户产品'
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset()
            const id = row.processId || this.ids
            getProcess(id).then((response) => {
                this.form = response.data
                this.open = true
                this.title = '编辑客户产品'
            })
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs['form'].validate((valid) => {
                if (valid) {
                    if (this.form.processId != null) {
                        updateProcess(this.form).then((response) => {
                            this.$modal.msgSuccess(
                                this.$t('tips.updateSuccess')
                            )
                            this.open = false
                            this.getList()
                        })
                    } else {
                        addProcess(this.form).then((response) => {
                            this.$modal.msgSuccess(this.$t('tips.addSuccess'))
                            this.open = false
                            this.getList()
                        })
                    }
                }
            })
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.processId || this.ids
            this.$modal
                .confirm(this.$t('process.confirmDelete'))
                .then(function () {
                    return delProcess(ids)
                })
                .then(() => {
                    this.getList()
                    this.$modal.msgError(this.$t('tips.deleteSuccess'))
                })
                .catch(() => {})
        },
        /** 导出按钮操作 */
        handleExport() {
            const queryParams = this.queryParams
            this.$modal
                .confirm(this.$t('process.export'))
                .then(() => {
                    this.exportLoading = true
                    return exportProcess(queryParams)
                })
                .then((response) => {
                    this.$download.name(response.msg)
                    this.exportLoading = false
                })
                .catch(() => {})
        },
        getListOrganize() {
            //   listOrganizeData().then(response => {
            //     this.organizesList = response.data
            //   })
        },
    },
}
</script>
